package com.example.json;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class ImageActivity extends AppCompatActivity {

    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);

        img = (ImageView) findViewById(R.id.imageView);

        new DownloadImageTask().execute("http://www.ynnu.edu.cn/images/139804914738585910.jpg",
                "http://www.ynnu.edu.cn/images/139804917810350403.jpg",
                "http://www.ynnu.edu.cn/images/139804904150763360.jpg",
                "http://www.ynnu.edu.cn/images/139804907553911498.jpg");
    }

    private InputStream openHttpConnection(String urlString) throws IOException{
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if(!(conn instanceof HttpURLConnection)){
            throw new IOException("Not an HTTP connection");
        }

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();

            if(response == HttpURLConnection.HTTP_OK){
                in = httpConn.getInputStream();
            }
        } catch (Exception e){
            Log.d("Networking", e.getLocalizedMessage());
            throw new IOException("Error connecting");
        }

        return  in;
    }

    private Bitmap DownloadImage(String URL){
        Bitmap bitmap = null;
        InputStream in = null;

        try{
            in = openHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e){
            Log.d("Networking", e.getLocalizedMessage());
        }

        return bitmap;
    }

    private class DownloadImageTask extends AsyncTask<String, Bitmap, Long> {

        @Override
        protected Long doInBackground(String... strings) {
            long imageCount = 0;

            for(int i = 0; i < strings.length; i++){
                Bitmap imageDownloaded = DownloadImage(strings[i]);

                if(imageDownloaded != null){
                    imageCount++;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e){
                        e.printStackTrace();
                    }

                    publishProgress(imageDownloaded);
                }
            }
            return imageCount;
        }

        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);

            Toast.makeText(getBaseContext(), "Total " + aLong + " images downloaded", Toast.LENGTH_LONG).show();
        }

        @Override
        protected void onProgressUpdate(Bitmap... values) {
//            super.onProgressUpdate(values);
            img.setImageBitmap(values[0]);

        }
    }
}
